package com.jkoss.wine.warehouse.controller;

import java.util.List;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.RequestMapping;

import com.baomidou.mybatisplus.mapper.Condition;
import com.baomidou.mybatisplus.mapper.Wrapper;
import com.baomidou.mybatisplus.plugins.Page;
import com.jkoss.base.controller.BaseController;
import com.jkoss.common.util.CommonUtil;
import com.jkoss.common.vo.DwzPageBean;
import com.jkoss.wine.config.mvc.MoneyExcelHandler;
import com.jkoss.wine.warehouse.service.IWareReportService;
import com.jkoss.wine.warehouse.vo.Balance;
import com.jkoss.wine.warehouse.vo.ReceiveDetails;
import com.jkoss.wine.warehouse.vo.ReceiveSummary;

import cn.afterturn.easypoi.entity.vo.NormalExcelConstants;
import cn.afterturn.easypoi.excel.entity.ExportParams;
import cn.afterturn.easypoi.excel.entity.enmus.ExcelType;
import cn.afterturn.easypoi.handler.inter.IExcelDataHandler;

/**
 * 仓库调拨单 前端控制器
 * 
 * @Author Jason
 * @Version 1.0, 2018-11-30
 * @See
 * @Since com.jkoss.wine.warehouse.controller
 * @Description: TODO
 */
@Controller
@RequestMapping("/warehouse/wareReport")
public class WareReportController extends BaseController {

	@Autowired
	private IWareReportService iWareReportService;

	/**
	 * 库存余额
	 * 
	 * @param dwzPageBean
	 * @param map
	 * @param request
	 * @param response
	 * @return
	 */
	@RequestMapping("/balanceList")
	@RequiresPermissions("/warehouse/wareReport/balanceList")
	public String balanceList(String pnname, String pnnum, DwzPageBean dwzPageBean, ModelMap map, HttpServletRequest request, HttpServletResponse response) {
		dwzPageBean.getCountResultMap().put("pnname", pnname);
		dwzPageBean.getCountResultMap().put("pnnum", pnnum);
		Wrapper wrapper = Condition.create().like("mpn.pnname", pnname).like("mpn.pnnum", pnnum);
		// 字段排序
		if (!CommonUtil.isBlank(dwzPageBean.getOrderField()) && !CommonUtil.isBlank(dwzPageBean.getOrderDirection())) {
			dwzPageBean.getCountResultMap().put("orderField", dwzPageBean.getOrderField());
			dwzPageBean.getCountResultMap().put("orderDirection", dwzPageBean.getOrderDirection());
			wrapper.orderBy(dwzPageBean.getOrderField(), CommonUtil.isEquals(dwzPageBean.getOrderDirection(), "asc"));
		}
		Page resultPage = iWareReportService.selectBalanceList(dwzPageBean.toPage(), wrapper);
		map.put("dwzPageBean", dwzPageBean.toDwzPageBean(resultPage));
		return "warehouse/wareReport/balanceList";
	}

	/**
	 * 库存余额导出EXCEL
	 * 
	 * @param map
	 * @param dwzPageBean
	 * @param request
	 * @param response
	 * @return
	 */
	@RequestMapping("/balanceExport")
	@RequiresPermissions("/warehouse/wareReport/balanceExport")
	public String balanceExport(ModelMap map, DwzPageBean dwzPageBean, HttpServletRequest request, HttpServletResponse response) {
		// 从数据库中查找数据
		Wrapper wrapper = Condition.create();
		// 根据用户id获取角色
		// boolean isDirector = true;
		// List<Role> roles = getRoleList();
		// for (Role role : roles) {
		// if ("运营主管".equals(role.getName())) {
		// isDirector = false;
		// }
		// }
		// if (isDirector) {
		// wrapper.eq("maid", getUserId());
		// }
		Page page = dwzPageBean.toPage();
		page.setSize(Page.NO_ROW_LIMIT);
		page.setCurrent(1);
		List list = iWareReportService.selectBalanceList(page, wrapper).getRecords();
		ExportParams params = new ExportParams(null, "库存余额", ExcelType.XSSF);
		IExcelDataHandler dataHandler = new MoneyExcelHandler();
		dataHandler.setNeedHandlerFields(new String[] { "商品单价", "库存总价" });
		params.setDataHandler(dataHandler);
		map.put(NormalExcelConstants.DATA_LIST, list); // 数据集合
		map.put(NormalExcelConstants.CLASS, Balance.class);// 导出实体
		map.put(NormalExcelConstants.PARAMS, params);// 参数
		map.put(NormalExcelConstants.FILE_NAME, "库存余额");// 文件名称
		return NormalExcelConstants.EASYPOI_EXCEL_VIEW;// View名称
	}

	/**
	 * 收发明细
	 * 
	 * @param dwzPageBean
	 * @param map
	 * @param request
	 * @param response
	 * @return
	 */
	@RequestMapping("/receiveDetailsList")
	@RequiresPermissions("/warehouse/wareReport/receiveDetailsList")
	public String receiveDetailsList(String onum, String type, String inwhname, String outwhname, String pnname, DwzPageBean dwzPageBean, ModelMap map, HttpServletRequest request, HttpServletResponse response) {
		dwzPageBean.getCountResultMap().put("onum", onum);
		dwzPageBean.getCountResultMap().put("type", type);
		dwzPageBean.getCountResultMap().put("inwhname", inwhname);
		dwzPageBean.getCountResultMap().put("outwhname", outwhname);
		dwzPageBean.getCountResultMap().put("pnname", pnname);
		Wrapper wrapper = Condition.create().like("a.onum", onum).like("a.type", type).like("a.inwhname", inwhname).like("a.outwhname", outwhname);

//		if (!CommonUtil.isBlank(pnname)) {
//			wrapper.andNew("(a.inpnname like {0} )", "'%" + pnname + "%'");
//		}
		// 字段排序
		if (!CommonUtil.isBlank(dwzPageBean.getOrderField()) && !CommonUtil.isBlank(dwzPageBean.getOrderDirection())) {
			dwzPageBean.getCountResultMap().put("orderField", dwzPageBean.getOrderField());
			dwzPageBean.getCountResultMap().put("orderDirection", dwzPageBean.getOrderDirection());
			wrapper.orderBy(dwzPageBean.getOrderField(), CommonUtil.isEquals(dwzPageBean.getOrderDirection(), "asc"));
		}
		Page resultPage = iWareReportService.selectReceiveDetailsList(dwzPageBean.toPage(), wrapper);
		map.put("dwzPageBean", dwzPageBean.toDwzPageBean(resultPage));
		return "warehouse/wareReport/receiveDetailsList";
	}

	/**
	 * 库存余额导出EXCEL
	 * 
	 * @param map
	 * @param dwzPageBean
	 * @param request
	 * @param response
	 * @return
	 */
	@RequestMapping("/receiveDetailsExport")
	@RequiresPermissions("/warehouse/wareReport/receiveDetailsExport")
	public String receiveDetailsExport(ModelMap map, DwzPageBean dwzPageBean, HttpServletRequest request, HttpServletResponse response) {
		// 从数据库中查找数据
		Wrapper wrapper = Condition.create();
		// 根据用户id获取角色
		// boolean isDirector = true;
		// List<Role> roles = getRoleList();
		// for (Role role : roles) {
		// if ("运营主管".equals(role.getName())) {
		// isDirector = false;
		// }
		// }
		// if (isDirector) {
		// wrapper.eq("maid", getUserId());
		// }
		Page page = dwzPageBean.toPage();
		page.setSize(Page.NO_ROW_LIMIT);
		page.setCurrent(1);
		List list = iWareReportService.selectReceiveDetailsList(page, wrapper).getRecords();
		ExportParams params = new ExportParams(null, "收发明细", ExcelType.XSSF);
		IExcelDataHandler dataHandler = new MoneyExcelHandler();
		dataHandler.setNeedHandlerFields(new String[] { "入库单价", "出库单价" });
		params.setDataHandler(dataHandler);
		map.put(NormalExcelConstants.DATA_LIST, list); // 数据集合
		map.put(NormalExcelConstants.CLASS, ReceiveDetails.class);// 导出实体
		map.put(NormalExcelConstants.PARAMS, params);// 参数
		map.put(NormalExcelConstants.FILE_NAME, "收发明细");// 文件名称
		return NormalExcelConstants.EASYPOI_EXCEL_VIEW;// View名称
	}

	/**
	 * 收发汇总
	 * 
	 * @param dwzPageBean
	 * @param map
	 * @param request
	 * @param response
	 * @return
	 */
	@RequestMapping("/receiveSummaryList")
	@RequiresPermissions("/warehouse/wareReport/receiveSummaryList")
	public String receiveSummaryList(String productNum, String name, String normName, DwzPageBean dwzPageBean, ModelMap map, HttpServletRequest request, HttpServletResponse response) {
		dwzPageBean.getCountResultMap().put("productNum", productNum);
		dwzPageBean.getCountResultMap().put("name", name);
		dwzPageBean.getCountResultMap().put("normName", normName);
		Wrapper wrapper = Condition.create().like("a.product_num", productNum).like("a.name", name).like("a.norm_name", normName);
		// 字段排序
		if (!CommonUtil.isBlank(dwzPageBean.getOrderField()) && !CommonUtil.isBlank(dwzPageBean.getOrderDirection())) {
			dwzPageBean.getCountResultMap().put("orderField", dwzPageBean.getOrderField());
			dwzPageBean.getCountResultMap().put("orderDirection", dwzPageBean.getOrderDirection());
			wrapper.orderBy(dwzPageBean.getOrderField(), CommonUtil.isEquals(dwzPageBean.getOrderDirection(), "asc"));
		}
		Page resultPage = iWareReportService.selectReceiveSummaryList(dwzPageBean.toPage(), wrapper);
		map.put("dwzPageBean", dwzPageBean.toDwzPageBean(resultPage));
		return "warehouse/wareReport/receiveSummaryList";
	}

	/**
	 * 库存余额导出EXCEL
	 * 
	 * @param map
	 * @param dwzPageBean
	 * @param request
	 * @param response
	 * @return
	 */
	@RequestMapping("/receiveSummaryExport")
	@RequiresPermissions("/warehouse/wareReport/receiveSummaryExport")
	public String receiveSummaryExport(ModelMap map, DwzPageBean dwzPageBean, HttpServletRequest request, HttpServletResponse response) {
		// 从数据库中查找数据
		Wrapper wrapper = Condition.create();
		// 根据用户id获取角色
		// boolean isDirector = true;
		// List<Role> roles = getRoleList();
		// for (Role role : roles) {
		// if ("运营主管".equals(role.getName())) {
		// isDirector = false;
		// }
		// }
		// if (isDirector) {
		// wrapper.eq("maid", getUserId());
		// }
		Page page = dwzPageBean.toPage();
		page.setSize(Page.NO_ROW_LIMIT);
		page.setCurrent(1);
		List list = iWareReportService.selectReceiveSummaryList(page, wrapper).getRecords();
		ExportParams params = new ExportParams(null, "收发汇总", ExcelType.XSSF);
		IExcelDataHandler dataHandler = new MoneyExcelHandler();
		dataHandler.setNeedHandlerFields(new String[] { "入库单价", "出库单价" });
		params.setDataHandler(dataHandler);
		map.put(NormalExcelConstants.DATA_LIST, list); // 数据集合
		map.put(NormalExcelConstants.CLASS, ReceiveSummary.class);// 导出实体
		map.put(NormalExcelConstants.PARAMS, params);// 参数
		map.put(NormalExcelConstants.FILE_NAME, "收发汇总");// 文件名称
		return NormalExcelConstants.EASYPOI_EXCEL_VIEW;// View名称
	}
}
